package com.sgb.link.view.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.google.android.material.tabs.TabLayout;
import com.sgb.link.R;
import com.sgb.link.entity.CommonPopEntity;

import java.util.List;

public class CustomXTabLayout extends TabLayout {
    int normalColor;
    int selectColor;
    int normalSize;
    int selectSize;
    boolean isBold;

    public CustomXTabLayout(Context context) {
        this(context, null);
    }

    public CustomXTabLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomXTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.normalColor = R.color.color_333333;
        this.selectColor = R.color.color_main;
        this.normalSize = 14;
        this.selectSize = 14;
        this.isBold = true;
    }

    public void setSelectColor(int selectColor) {
        this.selectColor = selectColor;
    }

    public void setNormalSize(int normalSize) {
        this.normalSize = normalSize;
    }

    public void setSelectSize(int selectSize) {
        this.selectSize = selectSize;
    }

    public void setBold(boolean bold) {
        isBold = bold;
    }

    public void setNormalColor(int normalColor) {
        this.normalColor = normalColor;
    }

    public void setNormalColor96() {
        this.normalColor = R.color.color_969696;
    }

    public void addOnTabSelectedListener(List<CommonPopEntity> mTitles, SimpleSelectedListener onTabSelectedListener) {
        clearOnTabSelectedListeners();
        for (int i = 0; i < mTitles.size(); i++) {
            Tab tab = getTabAt(i);
            if (tab != null) {
                tab.setCustomView(R.layout.item_tablayout_view);
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView tvTab = customView.findViewById(R.id.tv_tab_item);
                    tvTab.setText(mTitles.get(i).getTypeName());
                    if (i == 0) {
                        tvTab.setTextColor(ContextCompat.getColor(getContext(), selectColor));
                        tvTab.setTypeface(Typeface.defaultFromStyle(isBold ? Typeface.BOLD : Typeface.NORMAL));
                        tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectSize);
                    } else {
                        tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, normalSize);
                        tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        tvTab.setTextColor(ContextCompat.getColor(getContext(), normalColor));
                    }
                }
            }
        }
        addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView tvTab = customView.findViewById(R.id.tv_tab_item);
                    tvTab.setTextColor(ContextCompat.getColor(getContext(), selectColor));
                    tvTab.setTypeface(Typeface.defaultFromStyle(isBold ? Typeface.BOLD : Typeface.NORMAL));
                    tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectSize);
                    tvTab.invalidate();
                }
                if (onTabSelectedListener != null) {
                    onTabSelectedListener.onTabSelected(tab);
                }
            }

            @Override
            public void onTabUnselected(Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView tvTab = customView.findViewById(R.id.tv_tab_item);
                    tvTab.setTextColor(ContextCompat.getColor(getContext(), normalColor));
                    tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, normalSize);
                    tvTab.invalidate();
                }

            }

            @Override
            public void onTabReselected(Tab tab) {

            }
        });
    }

    public void addOnTabSelectedListener(String[] mTitles, SimpleSelectedListener onTabSelectedListener) {
        clearOnTabSelectedListeners();
        for (int i = 0; i < mTitles.length; i++) {
            Tab tab = getTabAt(i);
            if (tab != null) {
                tab.setCustomView(R.layout.item_tablayout_view);
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView tvTab = customView.findViewById(R.id.tv_tab_item);
                    tvTab.setText(mTitles[i]);
                    if (i == 0) {
                        tvTab.setTextColor(ContextCompat.getColor(getContext(), selectColor));
                        tvTab.setTypeface(Typeface.defaultFromStyle(isBold ? Typeface.BOLD : Typeface.NORMAL));
                        tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectSize);
                    } else {
                        tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, normalSize);
                        tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        tvTab.setTextColor(ContextCompat.getColor(getContext(), normalColor));
                    }
                }
            }
        }
        addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView tvTab = customView.findViewById(R.id.tv_tab_item);
                    tvTab.setTextColor(ContextCompat.getColor(getContext(), selectColor));
                    tvTab.setTypeface(Typeface.defaultFromStyle(isBold ? Typeface.BOLD : Typeface.NORMAL));
                    tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectSize);
                    tvTab.invalidate();
                }
                if (onTabSelectedListener != null) {
                    onTabSelectedListener.onTabSelected(tab);
                }
            }

            @Override
            public void onTabUnselected(Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView tvTab = customView.findViewById(R.id.tv_tab_item);
                    tvTab.setTextColor(ContextCompat.getColor(getContext(), normalColor));
                    tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, normalSize);
                    tvTab.invalidate();
                }
                if (onTabSelectedListener != null) {
                    onTabSelectedListener.onTabUnselected(tab);
                }
            }

            @Override
            public void onTabReselected(Tab tab) {
                if (onTabSelectedListener != null) {
                    onTabSelectedListener.onTabReselected(tab);
                }
            }
        });
    }

    /**
     * 选中加粗效果
     * @param onTabSelectedListener
     */
    public void addOnTabSelectedListener(SimpleSelectedListener onTabSelectedListener) {
        clearOnTabSelectedListeners();
        for (int i = 0; i < getTabCount(); i++) {
            Tab tab = getTabAt(i);
            if (tab != null) {
                tab.setCustomView(R.layout.item_tablayout_view);
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView tvTab = customView.findViewById(R.id.tv_tab_item);
                    tvTab.setText(tab.getText());
                    if (i == 0) {
                        tvTab.setTextColor(ContextCompat.getColor(getContext(), selectColor));
                        tvTab.setTypeface(Typeface.defaultFromStyle(isBold ? Typeface.BOLD : Typeface.NORMAL));
                        tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectSize);
                    } else {
                        tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, normalSize);
                        tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        tvTab.setTextColor(ContextCompat.getColor(getContext(), normalColor));
                    }
                }
            }
        }
        addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView tvTab = customView.findViewById(R.id.tv_tab_item);
                    tvTab.setTextColor(ContextCompat.getColor(getContext(), selectColor));
                    tvTab.setTypeface(Typeface.defaultFromStyle(isBold ? Typeface.BOLD : Typeface.NORMAL));
                    tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectSize);
                    tvTab.invalidate();
                }
                if (onTabSelectedListener != null) {
                    onTabSelectedListener.onTabSelected(tab);
                }
            }

            @Override
            public void onTabUnselected(Tab tab) {
                View customView = tab.getCustomView();
                if (customView != null) {
                    TextView tvTab = customView.findViewById(R.id.tv_tab_item);
                    tvTab.setTextColor(ContextCompat.getColor(getContext(), normalColor));
                    tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, normalSize);
                    tvTab.invalidate();
                }
                if (onTabSelectedListener != null) {
                    onTabSelectedListener.onTabUnselected(tab);
                }
            }

            @Override
            public void onTabReselected(Tab tab) {
                if (onTabSelectedListener != null) {
                    onTabSelectedListener.onTabReselected(tab);
                }
            }
        });
    }

    public static class SimpleSelectedListener implements OnTabSelectedListener {
        @Override
        public void onTabSelected(Tab tab) {

        }

        @Override
        public void onTabUnselected(Tab tab) {

        }

        @Override
        public void onTabReselected(Tab tab) {

        }
    }
}
